package com.sdry.approval.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdry.approval.constant.ApprovalTypeEnum;
import com.sdry.approval.entity.ForMySponsor;
import com.sdry.approval.entity.LeaveRequest;
import com.sdry.approval.entity.MeSponsor;
import com.sdry.approval.service.ForMySponsorService;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.system.entity.SysUser;
import com.sdry.system.service.UserService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: info_sdry
 * @description: 待我审批
 * @author: cb
 * @create: 2020-02-24 17:38
 **/
@Controller
@RequestMapping(value = "forMySponsor")
public class ForMySponsorController extends BaseController {

    @Autowired
    private ForMySponsorService forMySponsorService;

    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @return
     */
    @RequestMapping(value = "queryForMySponsorPage")
    @ResponseBody
    public TableDataInfo queryForMySponsorPage(ForMySponsor forMySponsor, HttpServletRequest request) {
        logger.info("获取待我审批的列表 sysDept = " + JSONObject.fromObject(forMySponsor));
        Page<ForMySponsor> meSponsorPage = new Page<>(forMySponsor.getPage(),forMySponsor.getLimit());
        QueryWrapper<ForMySponsor> queryWrapper = new QueryWrapper<>();
        SysUser user = (SysUser) request.getSession().getAttribute("user");
        if (StringUtils.isNotEmpty(forMySponsor.getRequestType())){
            queryWrapper.eq("request_type",forMySponsor.getRequestType()).eq("user_id",user.getUserId()).eq("complete_status",forMySponsor.getCompleteStatus());
        }else {
            queryWrapper.eq("user_id",user.getUserId()).eq("complete_status",forMySponsor.getCompleteStatus());
        }
        IPage<ForMySponsor> forMySponsorIPage = forMySponsorService.page(meSponsorPage, queryWrapper);
        List<ForMySponsor> forMySponsors = forMySponsorIPage.getRecords();
        for (ForMySponsor mySponsor : forMySponsors) {
            mySponsor.setRealName(userService.getById(mySponsor.getUserId()).getRealName());
        }
        return getDataTable(forMySponsors, forMySponsorIPage.getTotal());
    }


}
